/**
 * 搜索相关案例的页面的逻辑处理
 * Created by Administrator on 2016/12/29.
 */

module.exports = function ($scope, $state, caseBarDetailAnLiService, $anchorScroll, $location, $cookies, $rootScope, $stateParams) {
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate() + 1);

    //定义一个全局的页码
    var detailspagination = 0;

    //设置总页码
    var sumpaging = 0;

    var labelsrc = [];
    //条件字符串


    $scope.messages = $stateParams.messages;

    var objstr = $scope.messages;




    // 相关案例列表
    $scope.CorrelationCabe = function (index, pagesize, conditions) {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        caseBarDetailAnLiService.RefuseMsg(index, pagesize, conditions).then(function (data) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            var data1 = JSON.parse(data.data); //数据不规范所以要转一下对象
            sumpaging = data1.searchResult.totalCount
            setlistcase(data1);
            menuset(data1.menu);

            //设置分页
            setPage(data1.searchResult,index);

        }, function (response) {
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
            console.error('updatemessage error', response.status, response.statusText);
        });
    };

    $scope.init = function () {
        $scope.CorrelationCabe("0", "10", objstr);
    };
    $scope.init();
    // 增加标签
    $scope.rejectMsg = function (name, Country) {
        objstr = $scope.messages;
        addByValue(labelsrc, name, Country);
        setcondition(labelsrc);
        $scope.CorrelationCabe("0", "10", objstr);
        $scope.records = labelsrc;
        $scope.$apply();//需要手动刷新
    }

    //去除标签
    $scope.rejectMsg3 = function (Name, Country) {
        objstr = $scope.messages;
        removeByValue(labelsrc, Name, Country);
        //搜索刷新页面
        setcondition(labelsrc);
        $scope.CorrelationCabe("0", "10", objstr);
        $scope.records = labelsrc;

    };
    //去除实现
    function removeByValue(arr, Name) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].Name == Name) {
                arr.splice(i, 1);
                break;
            }
        }
    }

    //判断这个层级是否有标签如果有就把这个标签去掉
    function addByValue(arr, name, country) {
        var stc = false;
        var i2 = 0;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].Name + arr[i].Country === name + country) {
                stc = true;
                i2 = i;
                break;
            }
        }
        if (stc) {
            //条件存在
            // labelsrc.splice(i2, 1);

            // location.reload();
        } else {
            labelsrc.push({
                "Name": name,
                "Country": country
            }); // 添加到最后
        }
    }

    //拼接条件
    function setcondition(labelsrc) {

        for (var i = 0; i < labelsrc.length; i++) {
            objstr += "!%!" + labelsrc[i].Name + ":" + labelsrc[i].Country
        }
    }

    /* 列表处理*/
    function setlistcase(data1) {
        var table = "";
        var searchresult = data1.searchResult;

        var casejson = JSON.stringify(searchresult.judgements);

        $scope.correlation = JSON.parse(casejson);

    }


    function menuset(datameun) {

        // datameun
        //案由
        var menuCause = new Object();
        menuCause.name = "按案由";
        menuCause.childs = datameun.menuCause;

        //按地域
        var menuPca = new Object();
        menuPca.name = "按地域";
        menuPca.childs = datameun.menuPca;

        //按地域
        var menuJudgement = new Object();
        menuJudgement.name = "按法院层级";
        menuJudgement.childs = datameun.menuJudgement;

        var setting = {
            data: {
                key: {
                    children: "childs",
                }
            },
            callback: {
                onClick: zTreeOnClick
            },
            view: {
                showLine: false
            }
        };
        var zNodes = [menuCause, menuPca, menuJudgement];

        $(document).ready(function () {
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        });
        function zTreeOnClick(event, treeId, treeNode) {

            if (treeNode.level !== 0) {
                $scope.rejectMsg("案由", treeNode.name);
            }
        };
    }

    var GeneralPage = 0;

    //分页
    //请求完成后设置分页
    function setPage(data) {
        $scope.Pagelist = [];
        var index = data.index;

        // 每一页显示的数量
        var EachSize = 10;

        //总条数
        var PageSize = data.totalCount;

        //分页的数组


        //向上取整得到总页数
        GeneralPage = Math.ceil(data.totalCount / EachSize);


        $scope.GeneralPage = GeneralPage;

        var indexpage = data.index + 1;

        if(GeneralPage + 1>11){
            if(indexpage>5){
                   var yema=indexpage-4;
                   var zuihouyema=yema+10;
                   if(indexpage<GeneralPage-5){
                       for(var i=yema;i<zuihouyema;i++){
                           $scope.Pagelist.push(i);
                       }
                   }else {
                      // var yemaa=GeneralPage-9;
                       for(var i=yema;i<GeneralPage+1;i++){
                           $scope.Pagelist.push(i);
                       }
                   }

            }else{
                for(var i=1;i<11;i++){
                    $scope.Pagelist.push(i);
                }
            }
        }else {
            //取到分页数组
            for (var i = 1; i < GeneralPage + 1; i++) {
                $scope.Pagelist.push(i);
            }
        }




        $('.select').css('color', '#666');
        $scope.totalPage = data.pagesize;
        $scope.totalCount = PageSize; //总条数 已经ok
        $scope.totalPas =$scope.Pagelist;         //分页需要的数组
        $scope.page = indexpage;   //当前页

        setTimeout(function () {
            if ($scope.page === 0) {
                $('.select' + ($scope.page + 1)).css('color', 'red');
            } else {
                $('.select' + ($scope.page)).css('color', 'red');
            }

        }, 50);
    }

    /*用户点击分页点击改变页码*/
    $scope.changePage = function (currentPage) {
       /* if(currentPage>5){
            var yema=currentPage-4;
          for(var i=yema;currentPage+9;i++){
              $scope.Pagelist.push(i);
          }
        };
        $scope.totalPas =$scope.Pagelist;         //分页需要的数组*/
        if (currentPage <= GeneralPage && currentPage > -1) {

            // totalPage 页码
            $scope.CorrelationCabe(currentPage - 1, "10", objstr);

            //用户改变页码的时候保存下当前页面的下标
            detailspagination = currentPage;
            $anchorScroll.yOffset = 500;// 总是滚动额外的50像素
            // 将location.hash的值设置为
            // 你想要滚动到的元素的id
            $location.hash('ybcabelist');
            // 调用 $anchorScroll()
            $anchorScroll();

        } else {
            console.log('列表页码越界!');
        }
    }


    //跳转详情
    $scope.gocaseBarDeailAnliDetailController = function (downloadid, pagination, pagefloast) {
        pagination = detailspagination * 10 + pagination
            $state.go("caseBarDetailAnLiDetail2");

        $cookies.put('Condition', objstr, {
            expires: expireDate
        });

        /*     $cookies.put('location', location, {
         expires: expireDate
         });*/
        $cookies.put('pagination', pagination, {
            expires: expireDate
        });
        $cookies.put('sumpaging', sumpaging, {
            expires: expireDate
        });
        $cookies.put('downloadid', downloadid, {
            expires: expireDate
        });


    };


}
